package com.qumitech.visualnovel.service.user.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.qumitech.visualnovel.component.exception.BusinessException;
import com.qumitech.visualnovel.component.thirdapi.aliyun.AliYunOSSUtil;
import com.qumitech.visualnovel.component.util.HttpInvokeUtil;
import com.qumitech.visualnovel.model.common.BaseRsp;
import com.qumitech.visualnovel.model.common.ResultEnum;
import com.qumitech.visualnovel.model.dao.console.UserInfo;
import com.qumitech.visualnovel.model.common.UrlConstant;
import com.qumitech.visualnovel.pubdef.dto.user.UserContentFilterSystemDTO;
import com.qumitech.visualnovel.pubdef.dto.user.UserContentRecordAdminDTO;
import com.qumitech.visualnovel.pubdef.pubenum.AdminStateEnum;
import com.qumitech.visualnovel.service.exception.ExceptionService;
import com.qumitech.visualnovel.service.user.UserContentService;
import com.qumitech.visualnovel.service.user.constant.UserContentTypeEnum;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ywxie
 * @date 2019/9/23 16:44
 * @description
 */
@Service
public class UserContentServiceImpl implements UserContentService {

    private final String USER_CONTENT_URL = "/admin/usercontent";
    private final static String USER_CONTENT_FILTER_SYSTEMAVATAR = "/admin/usercontent/filter/system_avatar";
    private final static String USER_CONTENT_FILTER_VOICEROOMBACKGROUND = "/admin/usercontent/filter/voiceroom_background";


    @Autowired
    private ExceptionService exceptionService;


    @Override
    public Map<String, Object> listUserContentByConditions(Map<String, String> paramMap) {
        String url = UrlConstant.SERVER_URL + USER_CONTENT_URL;
        String body = HttpInvokeUtil.httpGet(url, paramMap, null);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        exceptionService.getFailureResultEnum(rsp);
        Map<String, Object> rspMap = (Map<String, Object>) rsp.getData();
        return rspMap;
    }

    @Override
    public void updateUserContentAdmin(UserContentRecordAdminDTO dto) {
        dto.setOperator(getOperatorName());
        String url = UrlConstant.SERVER_URL + USER_CONTENT_URL;
        String req = JSONObject.toJSONString(dto);
        String body = HttpInvokeUtil.httpPutJson(url, req, UrlConstant.DefaultEncode, null);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        exceptionService.getFailureResultEnum(rsp);
        if ((dto.getResourceUrl() != null && !dto.getResourceUrl().isEmpty()) && dto.getManualAuditState().equals(AdminStateEnum.DELETED.value())){
            List<String> newUrlList = new ArrayList<>();
            for (String videoUrl : dto.getResourceUrl()){
                if (videoUrl.contains(".gif")){
                    newUrlList.add(videoUrl.substring(0, videoUrl.indexOf(".gif")));
                } else {
                    newUrlList.add(videoUrl);
                }
            }
            if (!newUrlList.isEmpty()){
                //判断是否是系统头像
                List<String> filterSystemUrls = filterSystem(newUrlList,dto.getContentType());
                if(!filterSystemUrls.isEmpty()) {
                    AliYunOSSUtil.deleteObjectList(filterSystemUrls);
                }
            }
        }
    }

    private List<String> filterSystem(List<String> resourceUrls,Byte contentType){
        UserContentTypeEnum contentTypeEnum = UserContentTypeEnum.getContentEnumByType(contentType);
        if(contentTypeEnum == null){
            throw new BusinessException(ResultEnum.PARA_ERR);
        }
        List<UserContentFilterSystemDTO> filterSystemDTOS = new ArrayList<>();
        for(String url : resourceUrls){
            UserContentFilterSystemDTO filterSystemDTO = new UserContentFilterSystemDTO();
            filterSystemDTO.setContentName(url);
            filterSystemDTOS.add(filterSystemDTO);
        }
        List<String> newResourceUrls = new ArrayList<>();
        BaseRsp rsp;
        List<UserContentFilterSystemDTO> resultList;
        switch (contentTypeEnum){
            case Avatar:
                rsp = filterSystemAvatar(filterSystemDTOS);
                exceptionService.getFailureResultEnum(rsp);
                resultList = JSON.parseArray(JSON.toJSONString(rsp.getData()), UserContentFilterSystemDTO.class);
                for(UserContentFilterSystemDTO dto : resultList){
                    if(!dto.getSysExists()){
                        newResourceUrls.add(dto.getContentName());
                    }
                }
                return newResourceUrls;
            case VoiceRoomBackGroundImage:
                rsp = filterVoiceRoomBackGroundImage(filterSystemDTOS);
                exceptionService.getFailureResultEnum(rsp);
                resultList = JSON.parseArray(JSON.toJSONString(rsp.getData()), UserContentFilterSystemDTO.class);
                for(UserContentFilterSystemDTO dto : resultList){
                    if(!dto.getSysExists()){
                        newResourceUrls.add(dto.getContentName());
                    }
                }
                return newResourceUrls;
            default:
                return resourceUrls;
        }
    }

    @Override
    public BaseRsp filterSystemAvatar(List<UserContentFilterSystemDTO> dtos) {
        String url = UrlConstant.SERVER_URL + USER_CONTENT_FILTER_SYSTEMAVATAR;
        String req = JSON.toJSONString(dtos);
        String body = HttpInvokeUtil.httpPostJson(url, req, UrlConstant.DefaultEncode, null);
        return JSONObject.parseObject(body, BaseRsp.class);
    }

    @Override
    public BaseRsp filterVoiceRoomBackGroundImage(List<UserContentFilterSystemDTO> dtos) {
        String url = UrlConstant.SERVER_URL + USER_CONTENT_FILTER_VOICEROOMBACKGROUND;
        String req = JSON.toJSONString(dtos);
        String body = HttpInvokeUtil.httpPostJson(url, req, UrlConstant.DefaultEncode, null);
        return JSONObject.parseObject(body, BaseRsp.class);
    }

    private String getOperatorName() {
        Subject subject = SecurityUtils.getSubject();
        UserInfo activeUser = (UserInfo) subject.getPrincipal();
        return activeUser.getUsername();
    }
}
